//
//  RTJAutoMarkTimeAlertView.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/15.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJAutoMarkTimeAlertView.h"
#import "LewPopupViewController.h"

@interface RTJAutoMarkTimeAlertView ()

@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UIButton *btn3;
@property (weak, nonatomic) IBOutlet UIButton *btn4;
@property (strong, nonatomic) IBOutlet RTJAutoMarkTimeAlertView *bg_view;
@property (nonatomic,strong) id monthStr;
@property (weak, nonatomic) IBOutlet UILabel *lab1;
@property (weak, nonatomic) IBOutlet UILabel *lab2;
@property (weak, nonatomic) IBOutlet UILabel *lab3;
@property (weak, nonatomic) IBOutlet UILabel *lab4;
@property (nonatomic,strong) NSMutableArray *arrmonth;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (nonatomic,assign) BOOL isPayWay;
@property (nonatomic,strong) NSMutableArray *arr;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *h1;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *h2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *h3;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *top1;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *top2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *top3;
@end

@implementation RTJAutoMarkTimeAlertView

- (id)initWithFrame:(CGRect)frame isPayWay:(BOOL)isPayWay array:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSBundle mainBundle] loadNibNamed:[[self class] description] owner:self options:nil];
        _bg_view.frame = frame;
        [self addSubview:_bg_view];
        
        _arr = [NSMutableArray array];
        _bg_view.layer.masksToBounds = YES;
        _bg_view.layer.cornerRadius = 9;
        _bg_view.backgroundColor = [UIColor whiteColor];
        
       
        _isPayWay = isPayWay;
        _titleLab.text = _isPayWay ? @"还款方式":@"借款期限";
        
        if (_isPayWay) {
            
            [_arr addObjectsFromArray:array];
            
            if (array.count == 1) {
                
                self.h1.constant = 0;
                self.h2.constant = 0;
                self.h3.constant = 0;
                
                self.top1.constant = 0;
                self.top2.constant = 0;
                self.top3.constant = 0;
                
                self.btn2.hidden = YES;
                self.btn3.hidden = YES;
                self.btn4.hidden = YES;

                _bg_view.height = 265 -(21+13)*3;
                
            }else if (array.count == 2){
                
                self.h2.constant = 0;
                self.h3.constant = 0;
                
                self.top2.constant = 0;
                self.top3.constant = 0;
                
                self.btn3.hidden = YES;
                self.btn4.hidden = YES;
                
                 _bg_view.height = 265 -(21+13)*2;
                
            }else if (array.count == 3){
                
                self.h3.constant = 0;
                self.top3.constant = 0;
                
                self.btn4.hidden = YES;
                
                 _bg_view.height = 265 -(21+13)*1;
            }
            
            [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                if (idx ==0) {
                    
                    self.lab1.text = array[idx][@"name"];
                    
                }else if (idx==1){
                    
                    self.lab2.text = array[idx][@"name"];
                    
                }else if (idx==2){
                    
                    self.lab3.text = array[idx][@"name"];
                    
                }else if (idx==3){
                    
                    self.lab4.text = array[idx][@"name"];
                    
                }
            }];
            
        }else{
            
            _lab1.text = @"1个月";
            _lab2.text = @"3个月";
            _lab3.text = @"6个月";
            _lab4.text = @"12个月";

        }
        
        _arrmonth = [NSMutableArray array];
        
    }
    return self;
}

+ (instancetype)defaultPopupViewWithFrame:(CGRect)rect isPayWay:(BOOL)isPayWay array:(NSArray *)array{
    
    return [[RTJAutoMarkTimeAlertView alloc]initWithFrame:rect isPayWay:isPayWay array:array];
}

- (IBAction)allSelected:(id)sender {
    
    _btn1.selected = YES;
    _btn2.selected = YES;
    _btn3.selected = YES;
    _btn4.selected = YES;
    _lab1.textColor = [UIColor colorForHex:@"#FD3734"];
    _lab2.textColor = [UIColor colorForHex:@"#FD3734"];
    _lab3.textColor = [UIColor colorForHex:@"#FD3734"];
    _lab4.textColor = [UIColor colorForHex:@"#FD3734"];
    
    if (_isPayWay) {
        
        NSMutableArray *arr = [NSMutableArray array];
        
        for (NSDictionary *dic in _arr) {
            
            [arr addObject:dic[@"name"]];
            
        }
        
        NSString *str = [arr componentsJoinedByString:@","];
        _monthStr = str;

    }else{
        
        _monthStr = @"1,3,6,12";

    }
    
    [_arrmonth removeAllObjects];
    
    NSArray *arr =_isPayWay ? _arr : @[@"1",@"3",@"6",@"12"];
    
    [_arrmonth addObjectsFromArray:arr];
    
}

- (IBAction)selectedMonth:(UIButton *)sender {

    NSMutableArray *arrs = [NSMutableArray array];
    
    for (NSDictionary *dic in _arr) {
        
        [arrs addObject:dic];
        
    }
    
    NSArray *arr = _isPayWay ? arrs : @[@"1",@"3",@"6",@"12"];

        sender.selected =!sender.selected;
        
        if (sender.tag == 11) {
           
            if (sender.selected) {
                
                _lab1.textColor = [UIColor colorForHex:@"#FD3734"];
                _monthStr = arr[sender.tag-11];
                [self addMonth:_monthStr];
                
            }else{
                
                _lab1.textColor = [UIColor colorForHex:@"#333333"];
                _monthStr =  arr[sender.tag-11];
                [self removerMonth:_monthStr];
            }

        }
        
        if (sender.tag == 12) {
            
            if (sender.selected) {
                
                _lab2.textColor = [UIColor colorForHex:@"#FD3734"];
                _monthStr = arr[sender.tag-11];
                [self addMonth:_monthStr];
                
            }else{
                
                _lab2.textColor = [UIColor colorForHex:@"#333333"];
                _monthStr = arr[sender.tag-11];
                [self removerMonth:_monthStr];
            }

        }
        
        if (sender.tag == 13) {
            
            
            if (sender.selected) {
                
                _lab3.textColor = [UIColor colorForHex:@"#FD3734"];
                _monthStr = arr[sender.tag-11];
                [self addMonth:_monthStr];
                
            }else{
                
                _lab3.textColor = [UIColor colorForHex:@"#333333"];
                _monthStr = arr[sender.tag-11];
                [self removerMonth:_monthStr];
            }

        }
        
        if (sender.tag == 14) {
            
            if (sender.selected) {
                
                _lab4.textColor = [UIColor colorForHex:@"#FD3734"];
                _monthStr = arr[sender.tag-11];
                [self addMonth:_monthStr];
                
            }else{
                
                _lab4.textColor = [UIColor colorForHex:@"#333333"];
                _monthStr = arr[sender.tag-11];
                [self removerMonth:_monthStr];
            }

        }

}


-(void)removerMonth:(id)month{
    
    [_arrmonth enumerateObjectsUsingBlock:^(id str, NSUInteger idx, BOOL *stop) {
        
        if ([self.arrmonth containsObject:month]) {
            
            [self.arrmonth removeObjectAtIndex:idx];
            
        }
    }];
    
}

-(void)addMonth:(id)month{
    
    if (_arrmonth.count == 0) {
        
        [_arrmonth addObject:month];
        
    }else{
        
        [_arrmonth enumerateObjectsUsingBlock:^(NSString *str, NSUInteger idx, BOOL *stop) {
            
            if (![self.arrmonth containsObject:month]) {
                
                [self.arrmonth addObject:month];
                
            }
        }];
    }
    
}

- (IBAction)sure:(id)sender {
    
    if (_arrmonth.count == 0) {
        
        [SVProgressHUD showErrorWithStatus:_isPayWay ? @"请选择还款方式" : @"请选择借款期限"];
        return;
    }
    
    if (self.monthsureClick) {
        
        self.monthsureClick(self.arrmonth);
    }
    
    [_parentVC lew_dismissPopupView];
    

}


- (IBAction)cancel:(id)sender {
    
    [_parentVC lew_dismissPopupView];

}

-(void)withMonthSureClick:(monthBtnBlock)block{
    
    _monthsureClick = block;
}
@end
